/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * Console2.java * * Created on 22/09/2009, 12:58:16 PM */ package org.petah.spring.bai.gui; import java.awt.EventQueue; import java.awt.Font; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JScrollBar; import javax.swing.JScrollPane; import javax.swing.JTextPane; import org.petah.common.option.Option; import org.petah.common.option.OptionsManager; import org.petah.spring.bai.InformationLogger; /** * * @author Petah */ public class PConsole extends javax.swing.JPanel { // Options private static Option<Font> font = OptionsManager.getOption( new Option<Font>("PConsole.font", new Font("Courier New", Font.PLAIN, 10))); private static final String NEW_LINE = "\n"; // Class properties private Format format = Format.PlainText; private boolean outputUpdateWaiting = false; enum Format { HTML, PlainText } /** Creates new form Console2 */ public PConsole() { initComponents(); } public void setLogText(String s) { update(s, tpLog, spLog); } public void setProfileText(String s) { update(s, tpProfiler, spProfiler); } private void update(final String output, final JTextPane textPane, final JScrollPane scrollPane) { if (!outputUpdateWaiting) { outputUpdateWaiting = true; EventQueue.invokeLater(new Runnable() { public void run() { try { int scrollBarPos = scrollPane.getVerticalScrollBar().getValue(); switch (format) { default: textPane.setText(output); break; case HTML: textPane.setText("<html>" + output + "</html>"); break; } if (cbAutoScroll.isSelected()) { JScrollBar scrollBar = scrollPane.getVerticalScrollBar(); scrollBar.setValue(scrollBar.getMaximum()); } else { JScrollBar scrollBar = scrollPane.getVerticalScrollBar(); scrollBar.setValue(scrollBarPos); } } catch (Exception e) { } outputUpdateWaiting = false; } }); } } private String getNewLine() { switch (format) { default: return "\n"; case HTML: return "<br />"; } } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { toolBar = new javax.swing.JToolBar(); cbAutoScroll = new javax.swing.JCheckBox(); bReloadErrorLog = new javax.swing.JButton(); tabbedPane = new javax.swing.JTabbedPane(); spLog = new javax.swing.JScrollPane(); tpLog = new javax.swing.JTextPane(); spErrorLog = new javax.swing.JScrollPane(); tpErrorLog = new javax.swing.JTextPane(); spProfiler = new javax.swing.JScrollPane(); tpProfiler = new javax.swing.JTextPane(); setLayout(new java.awt.BorderLayout()); toolBar.setFloatable(false); toolBar.setRollover(true); cbAutoScroll.setSelected(true); cbAutoScroll.setText("Auto Scroll"); cbAutoScroll.setFocusable(false); cbAutoScroll.setOpaque(false); cbAutoScroll.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); toolBar.add(cbAutoScroll); bReloadErrorLog.setText("Reload Error Log"); bReloadErrorLog.setFocusable(false); bReloadErrorLog.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); bReloadErrorLog.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); bReloadErrorLog.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bReloadErrorLogActionPerformed(evt); } }); toolBar.add(bReloadErrorLog); add(toolBar, java.awt.BorderLayout.PAGE_START); tpLog.setEditable(false); tpLog.setFont(font.getValue()); spLog.setViewportView(tpLog); tabbedPane.addTab("Log", spLog); tpErrorLog.setEditable(false); tpErrorLog.setFont(font.getValue()); spErrorLog.setViewportView(tpErrorLog); tabbedPane.addTab("Error Log", spErrorLog); tpProfiler.setFont(font.getValue()); spProfiler.setViewportView(tpProfiler); tabbedPane.addTab("Profiler", spProfiler); add(tabbedPane, java.awt.BorderLayout.CENTER); }// </editor-fold>//GEN-END:initComponents private void bReloadErrorLogActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bReloadErrorLogActionPerformed FileInputStream in = null; try { String string = ""; String strLine; in = new FileInputStream(InformationLogger.getErrorLogFile()); BufferedReader br = new BufferedReader(new InputStreamReader(in)); while ((strLine = br.readLine()) != null) { string += strLine + getNewLine(); } tpErrorLog.setText(string); } catch (IOException ex) { tpErrorLog.setText("Error reading log file: " + InformationLogger.getErrorLogFile().getAbsolutePath()); Logger.getLogger(PConsole.class.getName()).log(Level.SEVERE, null, ex); } finally { try { in.close(); } catch (IOException ex) { Logger.getLogger(PConsole.class.getName()).log(Level.SEVERE, null, ex); } } }//GEN-LAST:event_bReloadErrorLogActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton bReloadErrorLog; private javax.swing.JCheckBox cbAutoScroll; private javax.swing.JScrollPane spErrorLog; private javax.swing.JScrollPane spLog; private javax.swing.JScrollPane spProfiler; private javax.swing.JTabbedPane tabbedPane; private javax.swing.JToolBar toolBar; private javax.swing.JTextPane tpErrorLog; private javax.swing.JTextPane tpLog; private javax.swing.JTextPane tpProfiler; // End of variables declaration//GEN-END:variables }